package yagwl.client.model;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

public abstract class FilteredItemListCache extends ItemListCache {

  private static Logger log = Logger.getLogger(FilteredItemListCache.class
      .getName());
  private List<FilterDTO> filters;
  protected List<FilterDTO> selectedFilters;

  public abstract void fetchFilters();

  protected void onLoadFiltersSuccess(List<FilterDTO> filterList) {
    log.info("onLoadFiltersSuccess: Entering");
    this.filters = filterList;
    try {
      DataReadyEventBus.getInstance().fireEvent(new FilterEvent(this));
    } catch (com.google.gwt.event.shared.UmbrellaException e) {
      Set<Throwable> es = e.getCauses();
      Iterator<Throwable> it = es.iterator();
      while (it.hasNext()) {
        Throwable t = it.next();
        log.severe("onLoadFiltersSuccess: " + t.toString());
        StackTraceElement[] stes = t.getStackTrace();
        for (StackTraceElement ste : stes)
          log.severe("onLoadFiltersSuccess: " + ste.getClassName() + "."
              + ste.getMethodName() + ":" + ste.getLineNumber());
      }
      throw e;
    }
    log.info("onLoadFiltersSuccess: FilterEvent raised: "
        + this.getClass().getName());
  }

  protected void onLoadFiltersFailure(Throwable caught) {
    log.info(caught.toString());
  }

  public List<FilterDTO> getFilters() {
    return filters;
  }

  public void setSelectedFilters(List<FilterDTO> selectedFilters) {
    this.selectedFilters = selectedFilters;
    onFilterSelection();
  }

  protected abstract void onFilterSelection();
}
